package edu.scau.misp.backend_module.core.rms_dto;

import edu.scau.misp.backend_module.domain.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderDTO {


    /**
     * 重新装载后端发往前端的结构数据；
     * 需要组合能够查询后端的变量，以及存放查询和结果的集合；
     */

    private Long orderId;

    private Date orderDate;
    private String orderRemark;
    private Long tableId;
    private Table table;
    private String orderStatus;




    private List<OrderItem> list = new ArrayList<>();

    private Long totalPrice;

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public String getOrderRemark() {
        return orderRemark;
    }

    public void setOrderRemark(String orderRemark) {
        this.orderRemark = orderRemark;
    }

    public Long getTableId() {
        return tableId;
    }

    public void setTableId(Long tableId) {
        this.tableId = tableId;
    }

    public Table getTable() {
        return table;
    }

    public void setTable(Table table) {
        this.table = table;
    }

    public List<OrderItem> getList() {
        return list;
    }

    public void setList(List<OrderItem> list) {
        this.list = list;
    }

    public Long getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Long totalPrice) {
        this.totalPrice = totalPrice;
    }

    @Override
    public String toString() {
        return "OrderDTO{" +
                "orderId=" + orderId +
                ", orderDate=" + orderDate +
                ", orderRemark='" + orderRemark + '\'' +
                ", tableId=" + tableId +
                ", table=" + table +
                ", list=" + list +
                ", totalPrice=" + totalPrice +
                '}';
    }

    public OrderDTO(Long orderId, Date orderDate, String orderRemark, Long tableId, Table table, List<OrderItem> list, Long totalPrice) {
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.orderRemark = orderRemark;
        this.tableId = tableId;
        this.table = table;
        this.list = list;
        this.totalPrice = totalPrice;
    }

    public OrderDTO() {
    }

    public OrderDTO(Order order){
        this.orderId = order.getOrderId();
        this.orderDate = order.getOrderDate();
        this.orderRemark = order.getOrderRemark();
        this.tableId = null;
        this.table = null;
        this.list = new ArrayList<>();
        //this.totalPrice = order.getTotalPrice();
    }
    public void addOrderItem(OrderItem item) {

        list.add(item);

    }

    public void addOrderDishAdditions(List<OrderDishAddition> orderDishAdditions) {

        if (orderDishAdditions != null) {

            this.list.addAll(orderDishAdditions);

        }

    }
    public void addOrderDeals(List<OrderDeal> orderDeals) {

        if (orderDeals != null) {

        }

    }

    public void calculateTotalPrice(){
        if(this.getTotalPrice()==null){
            this.setTotalPrice(0L);
        }

        for (OrderItem item : this.getList()) {

            if (item instanceof OrderDishAddition) {

                OrderDishAddition dishAddition = (OrderDishAddition) item;

                // 处理OrderDishAddition类型的逻辑
//                Long l1 = orderDTO.getTotalPrice();
//                Long l2 = dishAddition.getSubTotal().longValue();
//                Long tP = l1 +l2;
//                Long tP = orderDTO.getTotalPrice()+dishAddition.getSubTotal().longValue();
                this.setTotalPrice(this.getTotalPrice()+dishAddition.getSubTotal().longValue());




            }

            // ... 其他类型处理 ...

        }
    }
    public void formatOrderList(){
        // 遍历list列表

        for (Object item : this.getList()) {

            if (item instanceof OrderDishAddition) {

                // 如果是OrderDishAddition类型，则移除order字段

                OrderDishAddition orderDishAddition = (OrderDishAddition) item;

                orderDishAddition.setOrder(null);
                Dish dish = orderDishAddition.getDishAddition().getDish(); // 假设有getDish方法

                if (dish != null) {

                    dish.setCategory(null);
                }

                    // 如果Category内部还有属性需要移除，继续递归处理

            } else if (item instanceof OrderDeal) {

                // 如果是OrderDeal类型，则移除order字段

                OrderDeal orderDeal = (OrderDeal) item;

//                orderDeal.setOrder(null);
//                Deal deal = orderDeal.getDeal();

//                if (deal!=null){
//                    deal.setCategory(null);
//                }

            }

        }
    }


}
